<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	class girl extends CI_Controller {
		public function __construct()
		{
			parent::__construct();
			//$this->output->enable_profiler(TRUE);
			$this->load->helper('html');
			$this->load->helper('util');
			$this->load->library('form_validation');
			$this->load->config('config_ip');
			$this->load->library('pagination');
			$this->load->library('ckeditor');
			$this->load->library('ckfinder');
			$this->load->model('admin/login_model', 'login_model');		
			$this->load->model('admin/girl_model', 'girl_model');			
			session_start(); 
		}
		public function index( $offset = 0)
		{
			redirect('cms/girl/lst/');
		}
		public function lst( $offset = 0)
		{
			$sort_by = 'time_update'; $sort_order = 'desc';
			if(isset($_SESSION['userinfo']))
			{       
				$data = array();
				$data['fields'] = array(
					'gid' => 'ID',				
					'name' => 'Tên',
					'birthday' => 'ngày sinh',
					'interests' => 'Sở thích',		
					'talent' => 'Năng khiếu',	
					'image1' => 'Ảnh 1',			
					'image2' => 'Ảnh 2',
					'image3' => 'ẢNh 3',
					'image4' => 'Ảnh 4',
					'image5' => 'Ảnh 5',
					'status' => 'Trạng thái',
					'description' => 'Ghi chú',
					'time_update' => 'Thời gian cập nhật',
					);
				// Access data
				$limit = 10;
				$result = $this->girl_model->get_list_girl($limit, $offset, $sort_by, $sort_order);
				$data['rows'] = $result['rows'];
				$data['num_rows'] = $result['num_rows'];
				// Paging
				$segment_array=$this->uri->segment_array();
				$segment_count=$this->uri->total_segments();
				$config = array();
				$config['base_url'] = site_url("cms/girl/lst/");
				$config['total_rows'] = $data['num_rows'];
				$config['per_page'] = $limit;
				$config['uri_segment'] = 4;
				$this->pagination->initialize($config);
				$data['offset'] = $offset;
				$data['limit'] = $limit;
				$data['pagination'] = $this->pagination->create_links();
				// Sorting
				$data['sort_by'] = $sort_by;
				$data['sort_order'] = $sort_order;


				// Load view
				$this->load->view('admin/header_view',$data);
				$this->load->view('admin/girl_view');
				$this->load->view('admin/footer_view',$data);
				
			}
			else
			{
				redirect('/cms/', 'refresh');
			}
		}
		
		public function add()
		{
			$data = array();
			$data['title_form']="Thêm allbum cô gái";
			if(!isset($_SESSION['userinfo']))
			{       
				redirect('/cms/', 'refresh');				
			}
			if($this->input->post())
			{
				$image_path1="";
				$image_path2="";
				$image_path3="";
				$image_path4="";
				$image_path5="";
				if($_FILES['image1']['name']!=''){
					$data1=$this->upload_image('image1','girl');
					if($data1['errorCode']==200)
					{
						$image_path1=$data1['data'];
					}
					else
					{
						$data['alert']=$data1['errorMess'];
					}
				}
				if($_FILES['image2']['name']!=''){
					$data2=$this->upload_image('image2','girl');
					if($data2['errorCode']==200)
					{
						$image_path2=$data2['data'];
					}
					else
					{
						$data['alert']=$data2['errorMess'];
					}
				}
				if($_FILES['image3']['name']!=''){
					$data3=$this->upload_image('image3','girl');
					if($data3['errorCode']==200)
					{
						$image_path3=$data3['data'];
					}
					else
					{
						$data['alert']=$data3['errorMess'];
					}
				}
				if($_FILES['image4']['name']!=''){
					$data4=$this->upload_image('image4','girl');
					if($data4['errorCode']==200)
					{
						$image_path4=$data4['data'];
					}
					else
					{
						$data['alert']=$data4['errorMess'];
					}
				}
				if($_FILES['image5']['name']!=''){
					$data5=$this->upload_image('image5','girl');
					if($data5['errorCode']==200)
					{
						$image_path5=$data5['data'];
					}
					else
					{
						$data['alert']=$data5['errorMess'];
					}
				}
			
				$errorCode= $this->girl_model->insert_girl($this->input->post(),$image_path1,$image_path2,$image_path3,$image_path4,$image_path5);
				if(isset($errorCode))
				{
					if($errorCode==200)
					{
						redirect("cms/girl/");
					}
					else
					{
						$data['alert']=convert_errorcode($errorCode);
					}
				}		
			}
			
			
			
			$data['gid'] = 0;
			$data['name'] = "";
			$data['birthday'] = null;
			$data['interests'] = "";
			$data['talent'] = "";
			$data['image1'] = null;
			$data['image2'] = null;
			$data['image3'] = null;
			$data['image4'] = null;
			$data['image5'] = null;
			$data['status'] = 0;
			$data['description'] = "";
			$data['time_update'] = null;
			// Load view
			$this->load->view('admin/header_view',$data);
			$this->load->view('admin/girl_update_view',$data);
			$this->load->view('admin/footer_view',$data);
		}
		public function upload_image($name,$name_allbum)
		{
			if($_FILES[$name]['name']!=''){
				//upload thumbnail      $c
			
				$newFileName = $_FILES[$name]['name'];
			
				$fileExt = array_pop(explode(".", $newFileName));
				$fileOName = array_shift(explode(".", $newFileName));
				$fileOName = str_replace(" ", "_", $fileOName);
				$filename = $fileOName."_".time().".".$fileExt;

				$config['upload_path'] = 'uploads/'.$name_allbum.'/'.date("Y_m_d")."/";
				$config['file_name'] = $filename;
				$config['allowed_types'] = 'jpg|png|jpeg|gif';
				$config['max_size']  = 1024 * 80;
				//$config['encrypt_name'] = TRUE;

				if (!file_exists($config['upload_path'])) {
					mkdir($config['upload_path'], 0777, true);
				}


				$this->load->library('upload');
				$this->upload->initialize($config);
				if (!$this->upload->do_upload($name))
				{
					$data['errorCode'] =1;
					$data['errorMess'] =$this->upload->display_errors();;
					$data['data'] ="";
				}
				else
				{
					$upload_data = $this->upload->data();
					if($this->input->post($name)){
						unlink('uploads/'.$name_allbum.'/'.$this->input->post($name));
					}
					$img_file = date("Y_m_d")."/".$upload_data['file_name'];
					$data['errorCode'] =200;
					$data['errorMess'] ="";
					$data['data'] =$img_file;
				}
				
				return $data;
			} 	
		}
		
		public function edit($id){
			$data = array();
			if(!isset($_SESSION['userinfo']))
			{       
				redirect('/cms/', 'refresh');				
			}
			if($this->input->post())
			{			
				$post=$this->input->post();
				$image_path1=$post['image1'];
				$image_path2=$post['image2'];
				$image_path3=$post['image3'];
				$image_path4=$post['image4'];
				$image_path5=$post['image5'];
				if($_FILES['image1']['name']!=''){
					$data1=$this->upload_image('image1','girl');
					if($data1['errorCode']==200)
					{
						$image_path1=$data1['data'];
					}
					else
					{
						$data['alert']=$data1['errorMess'];
					}
				}
				if($_FILES['image2']['name']!=''){
					$data2=$this->upload_image('image2','girl');
					if($data2['errorCode']==200)
					{
						$image_path2=$data2['data'];
					}
					else
					{
						$data['alert']=$data2['errorMess'];
					}
				}
				if($_FILES['image3']['name']!=''){
					$data3=$this->upload_image('image3','girl');
					if($data3['errorCode']==200)
					{
						$image_path3=$data3['data'];
					}
					else
					{
						$data['alert']=$data3['errorMess'];
					}
				}
				if($_FILES['image4']['name']!=''){
					$data4=$this->upload_image('image4','girl');
					if($data4['errorCode']==200)
					{
						$image_path4=$data4['data'];
					}
					else
					{
						$data['alert']=$data4['errorMess'];
					}
				}
				if($_FILES['image5']['name']!=''){
					$data5=$this->upload_image('image5','girl');
					if($data5['errorCode']==200)
					{
						$image_path5=$data5['data'];
					}
					else
					{
						$data['alert']=$data5['errorMess'];
					}
				}
				$errorCode= $this->girl_model->update_girl($this->input->post(),$image_path1,$image_path2,$image_path3,$image_path4,$image_path5);
				if(isset($errorCode))
				{
					if($errorCode==200)
					{
						redirect("cms/girl/");
					}
					else
					{
						$data['alert']=convert_errorcode($errorCode);;
					}
				}				

			}
			$data['title_form']="Chỉnh sửa allbum cô gái";
			$girl_info = $this->girl_model->get_girl_by_id($id);
			$data['gid'] = $girl_info->gid;
			$data['name'] = $girl_info->name;
			$data['birthday'] = $girl_info->birthday;
			$data['interests'] = $girl_info->interests;
			$data['talent'] = $girl_info->talent;
			$data['image1'] =$girl_info->image1;
			$data['image2'] = $girl_info->image2;
			$data['image3'] = $girl_info->image3;
			$data['image4'] = $girl_info->image4;
			$data['image5'] = $girl_info->image5;
			$data['status'] = $girl_info->status;
			$data['description'] = $girl_info->description;
			$data['time_update'] = $girl_info->time_update;
			// Load view
			$this->load->view('admin/header_view',$data);
			$this->load->view('admin/girl_update_view',$data);
			$this->load->view('admin/footer_view',$data);
		}
		public function delete($id){
			$girl_data = $this->girl_model->get_girl_by_id($id);
			unlink('uploads/girl/'.$girl_data->image);
			$this->girl_model->delete_girl($id);
			redirect('cms/girl/');
		}
	}
